package xyz.xtt.pay.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.pay.api.dto.GetPayStatusMapRequest;
import xyz.xtt.pay.api.dto.GetPayStatusRequest;
import xyz.xtt.pay.api.enums.PayOrderStatusEnum;
import xyz.xtt.pay.client.request.ConfirmPayOrderRequest;
import xyz.xtt.pay.client.request.FetchUnPaidOrdersRequest;
import xyz.xtt.pay.client.response.TblPayOrderDto;
import xyz.xtt.pay.service.INftOrderService;
import xyz.xtt.pay.service.ITblPayOrderService;
import xyz.xtt.pay.vo.paycenter.PayStatusResult;

/**
 * @author dale
 * @date 2025/1/16
 **/
@Tag(name = "订单管理", description = "订单管理")
@RefreshScope
@Slf4j
@RequestMapping("/api/nft/pay/order")
@RestController
public class PayOrderController {
	@Autowired
	private INftOrderService nftOrderService;
	@Autowired
	private ITblPayOrderService tblPayOrderService;
	@Value("${pay.center.version:v2}")
	private String payCenterVersion;

	@Operation(summary = "查询待支付订单")
	@PostMapping("/fetchUnPaid")
	public ApiResult<List<TblPayOrderDto>> fetchUnPaidOrders(@RequestBody @Valid FetchUnPaidOrdersRequest fetchRequest) {
		return ApiResult.ok(tblPayOrderService.fetchUnPaidOrders(fetchRequest));
	}

	@Operation(summary = "支付订单状态确认")
	@PostMapping("/confirmPayStatus")
	public ApiResult<Boolean> confirmPayStatus(@RequestBody @Valid ConfirmPayOrderRequest confirmRequest) {
		return ApiResult.ok(nftOrderService.confirmPayStatusV2(confirmRequest));
	}

	@Operation(summary = "根据交易记录获取支付订单状态")
	@PostMapping("/getPayStatus")
	public ApiResult<Integer> getPayStatus(@RequestBody @NotNull GetPayStatusRequest request) {
		return ApiResult.ok(nftOrderService.getPayStatus(request));
	}

	@Operation(summary = "根据交易记录获取支付订单状态")
	@PostMapping("/getPayStatusMap")
	public ApiResult<Map<Long, Integer>> getPayStatusMap(@RequestBody @Valid GetPayStatusMapRequest request) {
		return ApiResult.ok(nftOrderService.getPayStatusMap(request));
	}

	@Operation(summary = "查询支付订单")
	@GetMapping("/fetch")
	public ApiResult<TblPayOrderDto> fetchPayOrder(@RequestParam(value = "payOrderId") Long payOrderId) {
		return ApiResult.ok(tblPayOrderService.fetchPayOrder(payOrderId));
	}

	@Operation(summary = "修复支付中心数据")
	@GetMapping("/fixPayCenterData")
	public ApiResult<Boolean> fixPayCenterData() {
		return ApiResult.ok(nftOrderService.fixPayCenterData());
	}

	@Operation(summary = "查询支付状态")
	@GetMapping("/getPayStatus")
	public ApiResult<PayStatusResult> getPayStatus(@RequestHeader("uid") String uid, @RequestParam @NotNull String orderId) {
		int invalidStrIndex = orderId.indexOf("?callback");
		if (invalidStrIndex > 0) {
			orderId = orderId.substring(0, invalidStrIndex);
		}
		GetPayStatusRequest request = new GetPayStatusRequest(null, null, Long.valueOf(orderId));
		int status = nftOrderService.getPayStatus(request);
		if (Objects.equals(status, PayOrderStatusEnum.PAY_SUCCESS.getCode())) {
			return ApiResult.ok(new PayStatusResult("0", "支付成功"));
		}
		return ApiResult.ok(new PayStatusResult("3", "支付中"));
	}

	@GetMapping("/skipRealPay")
	public ApiResult<Boolean> skipRealPay() {
		log.info("/api/nft/pay/order/skipRealPay");
		boolean skipRealPay = nftOrderService.skipRealPay();
		return ApiResult.ok(skipRealPay);
	}
}
